package com.weavernorth.util;

import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import sun.misc.BASE64Decoder;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class StoreServer {

    private static Log log = org.apache.commons.logging.LogFactory.getLog(StoreServer.class.getName());
    public static int photoStoreServer(String tp){
        BaseBean bb = new BaseBean();
        String newFilePathTwo="";
        String nameCode="";
        //转为json格式
        JSONObject tpJson = JSONObject.fromObject(tp);
        //获取content
        String contentCode= tpJson.getString("content");
        //获取图片name
        nameCode=tpJson.getString("name");
        if(tpJson.get("name")!=null&&!"null".equals(tpJson.get("name"))) {
            //存储到具体位置
            //获取存储图片一部分路径
            String  newFilePath = bb.getPropValue("porturl", "newFilePath");
            log.info("图片路径:"+newFilePath);
            //获取当前日期
            Calendar now = Calendar.getInstance();
            String  year=String.valueOf(now.get(Calendar.YEAR));
            log.info("年："+year);
            String  month=String.valueOf(now.get(Calendar.MONTH)+1);
            log.info("月："+month);
            //拼接存储图片路径
            newFilePathTwo =newFilePath+year+month;
            log.info("真正图片的存储路径："+newFilePathTwo);
            FileOutputStream fos = null;
            try {
                File file = new File(newFilePathTwo+nameCode);
                if (file.exists()) {
                    file.delete();
                }
                //解码
                BASE64Decoder decoder = new BASE64Decoder();
                byte[] bytes = decoder.decodeBuffer(contentCode);
                fos = new FileOutputStream(file);
                fos.write(bytes);
                fos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        //文件存放的位置集合
        List pathlst=new ArrayList();

        pathlst.add(newFilePathTwo+nameCode);
        log.info("文件的位置集合："+ pathlst);
        //保存后文件的名字
        List namelst=new ArrayList();
        namelst.add(nameCode);
        log.info("文件的名字："+namelst);
        //imagefileid  传空
        List imagefileidlst=new ArrayList();
        //目录id"  5065";
        String secid= Util.null2String(bb.getPropValue("porturl","secid"));
        //创建者id
        String userId="1";
        //目录列表显示的名字
        String requestname=nameCode;
        //调用工具类
        CreateFileUtilForImage cf = new CreateFileUtilForImage();
        //创建文档逻辑返回的id
        int docid = cf.doCreate(pathlst, namelst, imagefileidlst, secid, userId, requestname);
        log.info("创建文档逻辑返回的id是："+docid);

        return docid;
    }
}
